<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械退货</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<script type="text/javascript">
var editIndex = undefined;
//结束正在编辑的行
function endEditing() {
	if (editIndex == undefined) {
		return true
	}
	if ($('#qxcgReturnGoods').datagrid('validateRow', editIndex)) {
		$('#qxcgReturnGoods').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
//事件：单击单元格的事件
function onClickCell(index, field) {
	if (editIndex != index) {
		if (endEditing()) {
			$('#qxcgReturnGoods').datagrid('selectRow', index).datagrid('beginEdit',
					index);
			var ed = $('#qxcgReturnGoods').datagrid('getEditor', {
				index : index,
				field : field
			});
			if (ed) {
				($(ed.target).data('textbox') ? $(ed.target).textbox(
						'textbox') : $(ed.target)).focus();
			}
			editIndex = index;
		} else {
			setTimeout(function() {
				$('#qxcgReturnGoods').datagrid('selectRow', editIndex);
			}, 0);
		}
	}
}

//结束编辑，同步数据
function onEndEdit(index, row) {
	var ed = $(this).datagrid('getEditor', {
		index : index,
		field : 'hisReturngoods.thNumber'
	});
	if(ed){
		row.cgUnit = $(ed.target).combobox('getText');
	}else{
		row.cgUnit = null;
	}
}

//添加行
function append() {
	if (endEditing()) {
		$('#qxcgReturnGoods').datagrid('appendRow', {
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : '',
			'' : ''
		});
		editIndex = $('#qxcgReturnGoods').datagrid('getRows').length - 1;
		$('#qxcgReturnGoods').datagrid('selectRow', editIndex);
		$('#qxcgReturnGoods').datagrid('beginEdit',editIndex);
	}
}

//删除行
function removeit() {
	var ss=$('#qxcgReturnGoods').datagrid('getRowIndex');
	$('#qxcgReturnGoods').datagrid('deleteRow',ss);
	editIndex = undefined;
}

//应用
function accept() {
	if (endEditing()) {
		//$('#qxcgReturnGoods').datagrid('acceptChanges');
		endEditing();
	}
}

//撤消
function reject() {
	$('#qxcgReturnGoods').datagrid('rejectChanges');
	editIndex = undefined;
}

//获取数据
function getChanges() {
	var rows = $('#qxcgReturnGoods').datagrid('getChanges');
	alert(rows.length + ' rows are changed!');
}
$(function(){
	//采购器械自动赋值
	$('#qxcgth').combogrid({
		onChange: function (newValue, oldValue) {
			$.getJSON("selectHisPurchaseByID", {"cgNo": newValue}, function(data){
				$('#qxthcgph').textbox('setValue',data.cgBatchnumber);
				$('#qxthcgdj').numberbox('setValue',data.cgPrice);
				$('#qxthcgje').textbox('setValue',data.cgTotalprice);
				$('#qxthbz').textbox('setValue',data.cgRemarks);
				$.getJSON("selectHisSupplierByID", {"gysNo": data.gysNo}, function(data){
					$('#qxthgys').textbox('setValue',data.gysName);
				});
				$.getJSON("selectHisCheckandacceptByID", {"cgNo": data.cgNo}, function(data){
					$('#qxthsl').numberspinner('setValue',data.gysTel);
				});
			});
			var qxthcgdj=$('#qxthcgdj').numberbox('getValue');
			var qxthsl=$('#qxthsl').numberspinner('getValue');
			$('#qxthje').textbox('setValue',qxthcgdj*qxthsl);
			$('#qxthbz').textbox('clear');
		}
	});
	
	//退货金额
	$('#qxthsl').numberspinner({
		onChange: function (newValue, oldValue) {
			var qxthcgdj=$('#qxthcgdj').numberbox('getValue');
			$('#qxthje').textbox('setValue',qxthcgdj*newValue);
		}
	});
	
	//只能输入电话号码,设置text须要验证
	$("input[type=text]").validatebox();
	$.extend($.fn.validatebox.defaults.rules, {
		mobile : {
			// 验证手机号码
			validator : function(value) {
				return /^(13|15|18)\d{9}$/i.test(value);
			},
			message : '只能是11位的手机号码'
		},englishCheckSub : {
			validator : function(value) {
				return /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{6,20})$/.test(value);
			},
			message : '密码中必须包括数字和字母'
	},});
	
	//采购产品加入临时退货表
	$('#add-thData').linkbutton({
		onClick : function() {
			var qxcgthID=$('#qxcgth').combogrid('getValue');
			var qxcgthName=$('#qxcgth').combogrid('getText');
			var qxthcgph=$('#qxthcgph').textbox('getValue');
			var qxthgys=$('#qxthgys').combogrid('getValue');
			var qxthcgdj=$('#qxthcgdj').numberbox('getValue');
			var qxthsl=$('#qxthsl').numberspinner('getValue');
			var qxthje=$('#qxthje').numberbox('getValue');
			var qxthbz=$('#qxthbz').textbox('getValue');
			var thdate=$('#thdate').datebox('getValue');
			var thdh=$('#thdh').textbox('getValue');
			var thlxr=$('#thlxr').textbox('getValue');
			var thfzr=$('#thfzr').combogrid('getValue');
			
			var rows=$('#qxcgReturnGoods').datagrid('getRows');
			var ii=0;
			for(var i=0;i<rows.length;i++){
				//alert(cgqx);
				var name=rows[i].cqName;
				//alert(name);
				if(name==qxcgthName){
					ii++;
				}
			}
			if(thdate=='' || thdh=='' || thlxr=='' || thfzr=='' ||
					qxcgthName=='' || qxthcgph=='' || qxthgys=='' || qxthcgdj=='' || qxthsl=='' || qxthje=='' ){
				$.messager.alert("温馨提示", "请把数据填充完整!",'info');
			}else if(ii>0){
				$.messager.alert("提示", "产品已存在临时退货中，请勿重复添加!");
				$('#thAllqx').form("clear");
			}else{
				$('#qxcgReturnGoods').datagrid('appendRow', {
					'hisReturngoods.cgNo' : qxcgthID,
					'cqName' : qxcgthName,
					'hisReturngoods.cgPrice' : qxthcgdj,
					'hisReturngoods.thSupplier' : qxthgys,
					'hisReturngoods.thNumber' : qxthsl,
					'hisReturngoods.thMoney' : qxthje,
					'hisReturngoods.cgBatchnumber' : qxthcgph,
					'hisReturngoods.thRemarks' : qxthbz
				});
				$('#thAllqx').form("clear");
			}
		}
	});
	
	//退货
	$('#submit-qxth').linkbutton({
		onClick : function() {
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var rows = $('#qxcgReturnGoods').datagrid('getChanges');
			//为每一行生成对应的隐藏表单域
			var qxthtype=$('#qxthtype').combobox('getValue');
			var qxthstate='未退货';
			var thdate=$('#thdate').datebox('getValue');
			var thdh=$('#thdh').textbox('getValue');
			var thlxr=$('#thlxr').textbox('getValue');
			var thfzr=$('#thfzr').combogrid('getValue');
			var today='${ReturngoodId }'*1;
			var ff_thpQXReturnGoods=$('#ff_thpQXReturnGoods');
			for(var i = 0; i < rows.length; i ++){
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].thNo').val(
								today+i).appendTo(ff_thpQXReturnGoods);
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].thTel').val(
								thdh).appendTo(ff_thpQXReturnGoods);
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].thState').val(
								qxthstate).appendTo(ff_thpQXReturnGoods);
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].thType').val(
								qxthtype).appendTo(ff_thpQXReturnGoods);
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].thTime').val(
								thdate).appendTo(ff_thpQXReturnGoods);
				$("<input type='hidden' x='y'>").attr("name",
						"hisReturngoods[" + i + '].ygNo').val(
								thfzr).appendTo(ff_thpQXReturnGoods);
				generateHidden(rows[i], "cgNo", i);
				generateHidden(rows[i], "cgPrice", i);
				generateHidden(rows[i], "thMoney", i);
				generateHidden(rows[i], "thNumber", i);
				generateHidden(rows[i], "thRemarks", i);
				generateHidden(rows[i], "thSupplier", i);
			}
			//提交表单
			if(rows.length != 0){
				$('#ff_thpQXReturnGoods').form('submit', {
					success: function(v){
						var json = eval("(" + v + ")");
						//alert(json);
						if(json=='操作成功'){
							$(":hidden[x='y']").remove();
							$('#qxcgReturnGoods').datagrid('loadData', { total: 0, rows: [] });
							$.messager.alert('温馨提示', "退货"+json);
							$('#ff_thpQXReturnGoods').form("clear");
							$("#win-QiXieReturnGoods").window("close");
						}else{
							$.messager.alert('温馨提示', json,'info');
						}
					}
				});
			}
		}
	});
	function generateHidden(row, name, i) {
		var ff_thpQXReturnGoods = $("#ff_thpQXReturnGoods");
		$("<input type='hidden' x='y'>").attr("name",
				"hisReturngoods[" + i + '].'  + name).val(
				row['hisReturngoods.'+name]).appendTo(ff_thpQXReturnGoods);
	}
});
</script>
<style type="text/css">
	*{
		font-size:12px;
	}
	#ReturnGoodsContent{
		padding-bottom: 10px;
	}
	#ReturnGoodsInfo{
		width:100%;
		overflow: hidden;
	}
	li{
		float: left;
		list-style: none;
	}
</style>

	<!-- 器械退货 -->
	<!-- <div id="win-QiXieReturnGoods" class="easyui-window" title="器械退货" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-returnGoods'"
		style="width: 806px; height: 500px;"> -->
		<form id="ff_thpQXReturnGoods" action="QiXieReturnGoods" method="post">
			<div id="ReturnGoodsContent">
				<div id="ReturnGoodsInfo" style="background-color: rgb(236,243,255)">
					<ul>
						<li style="margin:0px 40px 5px 0px;">器械退货号&nbsp;<input value="${ReturngoodId }" class="easyui-textbox" data-options="width: 150" readonly="readonly"></li>
						<li style="margin:0px 40px 5px 0px;">状态&nbsp;<select id="qxthstate" class="easyui-combogrid" data-options="width: 80" readonly="readonly">
							<option value="未退货" selected="selected">未退货</option><option value="已退货">已退货</option></select></li>
						<li style="margin:0px 0px 5px 0px;">退货类型&nbsp;<select id="qxthtype" class="easyui-combobox" data-options="width: 80,panelHeight:'auto'">
							<option value="器械" selected="selected">器械</option><option value="耗材">耗材</option>
								<option value="中药">中药</option><option value="西药">西药</option></select></li>
						<li style="margin:0px 0px 5px 40px;">退货日期&nbsp;<input id="thdate" class="easyui-datebox" data-options="width: 100"></li>
						<li style="margin:0px 128px 10px 12px;">退货电话&nbsp;<input id="thdh" class="easyui-textbox" data-options="width: 100" validtype="mobile" type="text"></li>
						<li style="margin:0px 80px 10px 0px;">退货联系人&nbsp;<input id="thlxr" class="easyui-textbox" data-options="width: 100"></li>
						<li style="margin:0px 0px 10px 50px;">负责人&nbsp;<select id="thfzr" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployee',
							idField:'ygNo',
							panelHeight:200,
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></select></li>
					</ul>
				</div>
				<div style="border-top:1px solid #95B8e7;width:100%;height:345px;overflow: hidden;">
				        <div>
				        	<div id="thAllqx" style="overflow: hidden;" >
				        		<ul>
									<li style="margin:0px 85px 5px 0px;">器械&nbsp;<select id="qxcgth" class="easyui-combogrid" data-options="width: 200,
										url:'selectAllHisPurchaseByName',
										idField:'cgNo',
										panelHeight:'auto',
										textField:'cpName',
										columns:[[
								            {field:'cpName',title:'采购器械名称',width:'180px'}
										]]"></select></li>
									<li style="margin:0px 0px 5px 0px;">批号&nbsp;<input id="qxthcgph" class="easyui-textbox" readonly="readonly" data-options="width: 150"></li>
									<li style="margin:0px 0px 5px 53px;">供应商&nbsp;<select id="qxthgys" class="easyui-combogrid" readonly="readonly" data-options="width: 150"></select></li>
									<li style="margin:0px 85px 5px -24px;">采购单价&nbsp;<input id="qxthcgdj" class="easyui-numberbox" data-options="width: 150,precision:2" readonly="readonly"></li>
									<li style="margin:0px 0px 5px 25px;">退货数量&nbsp;<input id="qxthsl" class="easyui-numberspinner" data-options="width: 150,min:1,editable:false" readonly="readonly"></li>
									<li style="margin:0px 0px 5px 42px;">退货金额&nbsp;<input id="qxthje" class="easyui-numberbox" data-options="width: 150" readonly="readonly"></li>
									<li style="margin:0px 0px 10px 0px;">备注&nbsp;<input id="qxthbz" class="easyui-textbox" data-options="width: 550"></li>
									<li style="margin:0px 0px 10px 75px;"><a id="add-thData" class="easyui-linkbutton" style="width:80px;">加入</a></li>
								</ul>
				        	</div>
				        	<div style="border:0px red solid;height:240px;">
								<table id="qxcgReturnGoods" class="easyui-datagrid"
									data-options="
										fitColumns:true,
										fit:true,
										singleSelect: true,
										rownumbers: true,
										pagination: true,
										pageSize: 10,
										toolbar: '#qxthtb',
										pageList: [5,10,15,20],
										border:false
										">
									<thead>
										<tr>
											<th data-options="field:'hisReturngoods.cgNo',width:'13%',editor:'numberbox'">器械编号</th>
											<th data-options="field:'cqName',width:'15%',editor:'numberbox'">器械名称</th>
											<th data-options="field:'hisReturngoods.cgPrice',width:'10%',editor:'numberbox'">采购单价</th>
											<th data-options="field:'hisReturngoods.thSupplier',width:'15%',editor:'textbox'">供应商</th>
											<th data-options="field:'hisReturngoods.thNumber',width:'8%',editor:'numberbox'">退货数量</th>
											<th data-options="field:'hisReturngoods.thMoney',width:'8%',editor:'numberbox'">退货金额</th>
											<th data-options="field:'hisReturngoods.cgBatchnumber',width:'12%',editor:'textbox'">批号</th>
											<th data-options="field:'hisReturngoods.thRemarks',width:'20%',editor:'textbox'">备注</th>
										</tr>
									</thead>
								</table>
								<div id="qxthtb" style="height: auto;padding-left: 30px;">
									<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-add',plain:true" onclick="append()">添加一行</a>
									<a href="javascript:void(0)" class="easyui-linkbutton"
										data-options="iconCls:'icon-remove',plain:true"
										onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
										class="easyui-linkbutton"
										data-options="iconCls:'icon-ok',plain:true" onclick="accept()">应用</a>
									<a href="javascript:void(0)" class="easyui-linkbutton"
										data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
								</div>
				        	</div>
				        </div>
				    </div>
				    <div style="margin:5px 5px 0px 0px;text-align: right;">
						<a id="submit-qxth" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">退货</a>
						<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:$('#win-QiXieReturnGoods').window('close');" onclick="">取消</a>
					</div>
				</div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>